/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entity.MHospital;
import java.math.BigDecimal;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Munliga
 */
@Stateless
public class MHospitalFacade extends AbstractFacade<MHospital> {
    @PersistenceContext(unitName = "MedicalHub-warPU")
    private EntityManager em;
    @EJB
    private MHospitalFacade mHospitalFacade;

    protected EntityManager getEntityManager() {
        return em;
    }

    public MHospitalFacade() {
        super(MHospital.class);
    }
    
    public List<MHospital> LoadDataHospital(String txtName, String txtLocation, String txtHospitalLvl, String sortIndex, String sortDirection) {
        try {
            String strQuery = "SELECT m FROM MHospital m ";
            String strWhere = " Where 1=1 ";
            strWhere += (!txtName.isEmpty()) ? " AND m.name Like '%" + txtName + "%' " : " ";
            strWhere += (!txtLocation.isEmpty()) ? " AND m.location Like '%" + txtLocation + "%' " : " ";
            strWhere += (!txtHospitalLvl.isEmpty()) ? " AND m.hospitalLevel Like '%" + txtHospitalLvl + "%' " : " ";
            strWhere += "ORDER BY m." + sortIndex + " " + sortDirection;
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();
        } catch (Exception e) {
            return null;
        }
    }
    public List<MHospital> findByHospitalId(String id) {
        return em.createNamedQuery("MHospital.findById").setParameter("id", Long.parseLong(id)).getResultList();
    }

    public void EditHospital(String id, String txtName, String txtLocation, String txtHospitalLvl, String txtLatitude, String txtLongtitude){
        MHospital editHospital = mHospitalFacade.find(Long.parseLong(id));
        editHospital.setName(txtName);
        editHospital.setLocation(txtLocation);
        editHospital.setHospitalLevel(txtHospitalLvl);
        if (!txtLatitude.isEmpty()){
            editHospital.setLatitude(new BigDecimal(txtLatitude));
        }else{
            editHospital.setLatitude(null);
        }
        if (!txtLongtitude.isEmpty()){
            editHospital.setLongtitude(new BigDecimal(txtLongtitude));
        }else{
            editHospital.setLongtitude(null);
        }
        em.persist(editHospital);
    }

    public void DeleteHospital(String id){
        MHospital deleteHospital = mHospitalFacade.find(Long.parseLong(id));
        em.remove(deleteHospital);
    }

    public void AddHospital(String txtName, String txtLocation, String txtHospitalLvl, String txtLatitude, String txtLongtitude){
        MHospital addHospital = new MHospital();
        addHospital.setName(txtName);
        addHospital.setLocation(txtLocation);
        addHospital.setHospitalLevel(txtHospitalLvl);
        if (!txtLatitude.isEmpty()){
            addHospital.setLatitude(new BigDecimal(txtLatitude));
        }else{
            addHospital.setLatitude(null);
        }
        if (!txtLongtitude.isEmpty()){
            addHospital.setLongtitude(new BigDecimal(txtLongtitude));
        }else{
            addHospital.setLongtitude(null);
        }
        em.persist(addHospital);
    }
    
    public List<MHospital> SearchDataHospital(String txtKeyword) {
        try {
            String strQuery = "SELECT m FROM MHospital m ";
            String strWhere = " Where 1=1 ";
            strWhere += (!txtKeyword.isEmpty()) ? " AND CONCAT(m.name,' ',m.location,' ',m.hospitalLevel) Like '%" + txtKeyword + "%' " : " ";
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();
        } catch (Exception e) {
            return null;
        }
    }
}
